package com.yjz.study.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.yjz.common.restful.CallBack
import com.yjz.common.restful.HttpBaseResponse
import com.yjz.common.restful.JCall
import com.yjz.study.http.ApiFactory
import com.yjz.study.http.api.NoticeApi
import com.yjz.study.http.entity.Resource
import com.yjz.study.http.entity.res.CourseNotice
import com.yjz.study.http.entity.res.Notice

/**
 *作用 :
 *作者 : yjz
 *时间 : 2025/5/23 15:24
 */
class CourseViewModel : ViewModel() {

    fun requestCourseNoticeData(): MutableLiveData<Resource<List<Notice>>> {
        val noticeLiveData = MutableLiveData<Resource<List<Notice>>>()
        ApiFactory.create(NoticeApi::class.java).notice().enqueue(object : CallBack<CourseNotice> {
            override fun onSuccess(response: HttpBaseResponse<CourseNotice>) {
                if (response.successful()) {
                    val courseNotice = response.data
                    if (courseNotice == null || courseNotice.list.isNullOrEmpty()) {
                        noticeLiveData.value = Resource(
                            status = Resource.Status.ERROR,
                            msg = response.msg!!,
                            data = arrayListOf()
                        )
                    } else {
                        noticeLiveData.value = Resource(
                            status = Resource.Status.SUCCESS,
                            msg = response.msg!!,
                            data = courseNotice.list
                        )
                    }
                } else {
                    noticeLiveData.value = Resource(
                        status = Resource.Status.ERROR,
                        msg = response.msg!!
                    )
                }
            }

            override fun onFailed(throwable: Throwable) {
                noticeLiveData.value = Resource(
                    status = Resource.Status.ERROR,
                    msg = throwable.message!!
                )
            }
        })

        return noticeLiveData
    }
}